package com.ruoyi.tb.lmt.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.GuidUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tb.lmt.domain.bo.TbFdxBo;
import com.ruoyi.tb.lmt.domain.bo.TbFdxdetailBo;
import com.ruoyi.tb.lmt.domain.bo.TbMutualInspectionBo;
import com.ruoyi.tb.lmt.domain.vo.TbLmtrepairWorkdetailVo;
import com.ruoyi.tb.lmt.service.ITbMutualInspectionService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbMutualInspectiondetailVo;
import com.ruoyi.tb.lmt.domain.bo.TbMutualInspectiondetailBo;
import com.ruoyi.tb.lmt.service.ITbMutualInspectiondetailService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 互检详情
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/mutualInspectiondetail")
public class TbMutualInspectiondetailController extends BaseController {

    private final ITbMutualInspectiondetailService iTbMutualInspectiondetailService;

    @Autowired
    private ITbMutualInspectionService tbMutualInspectionService;

    /**
     * 查询互检详情列表
     */
    @GetMapping("/list")
    public TableDataInfo<TbMutualInspectiondetailVo> list(TbMutualInspectiondetailBo bo, PageQuery pageQuery) {
        return iTbMutualInspectiondetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出互检详情列表
     */
    @Log(title = "互检详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbMutualInspectiondetailBo bo, HttpServletResponse response) {
        List<TbMutualInspectiondetailVo> list = iTbMutualInspectiondetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "互检详情", TbMutualInspectiondetailVo.class, response);
    }

    /**
     * 获取互检详情详细信息
     *
     * @param ID 主键
     */
    @SaCheckPermission("xha-lmt:mutualInspectiondetail:query")
    @GetMapping("/{ID}")
    public R<TbMutualInspectiondetailVo> getInfo(@NotNull(message = "主键不能为空")
                                                 @PathVariable Long ID) {
        return R.ok(iTbMutualInspectiondetailService.queryById(ID));
    }

    /**
     * 新增互检详情
     */
    @SaCheckPermission("xha-lmt:mutualInspectiondetail:add")
    @Log(title = "互检详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbMutualInspectiondetailBo bo) {
        return toAjax(iTbMutualInspectiondetailService.insertByBo(bo));
    }

    /**
     * 修改互检详情
     */
    @SaCheckPermission("xha-lmt:mutualInspectiondetail:edit")
    @Log(title = "互检详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbMutualInspectiondetailBo bo) {
        return toAjax(iTbMutualInspectiondetailService.updateByBo(bo));
    }

    /**
     * 删除互检详情
     *
     * @param IDs 主键串
     */
    @SaCheckPermission("xha-lmt:mutualInspectiondetail:remove")
    @Log(title = "互检详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] IDs) {
        return toAjax(iTbMutualInspectiondetailService.deleteWithValidByIds(Arrays.asList(IDs), true));
    }

    @SaCheckPermission("xha-lmt:mutualInspectiondetail:addMutual")
    @Log(title = "新增互检详情任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addMutual")
    public R<Void> addMutual(@Validated(AddGroup.class) @RequestBody Map map) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TbMutualInspectionBo tbMuBo = new TbMutualInspectionBo();
            tbMuBo.setId(map.get("mutualId") == null ? null : Long.parseLong((String) map.get("mutualId")));
            String requireStartTime = (String) map.get("repairStartDate");
            if (requireStartTime != null && !requireStartTime.isEmpty()) {
                tbMuBo.setRepairStartDate(formatter.parse(requireStartTime));
            }
            tbMutualInspectionService.updateByBo(tbMuBo);
            TbMutualInspectiondetailBo tbMutualBo = new TbMutualInspectiondetailBo();
            tbMutualBo.setMutualId(tbMuBo.getId());
            tbMutualBo.setWorkName((String) map.get("workName"));
            tbMutualBo.setWorkKind((String) map.get("workKind"));
            String workGroupIdString = (String) map.get("workCategoryId");
            if (workGroupIdString != null && !workGroupIdString.isEmpty()) {
                try {
                    Long workGroupId = Long.parseLong(workGroupIdString);
                    tbMutualBo.setWorkCategoryId(workGroupId);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
            tbMutualBo.setWorkCategory((String) map.get("workCategory"));
            tbMutualBo.setWorkCategoryRemark((String) map.get("workCategoryRemark"));
            tbMutualBo.setStatus("未提报");
            tbMutualBo.setCheckReapirReqire((String) map.get("checkReapirReqire"));
            tbMutualBo.setTaskUrl((String) map.get("taskUrl"));
            tbMutualBo.setCreateTime(new Date());
            LoginUser currentUser = LoginHelper.getLoginUser();
            tbMutualBo.setCreateBy(currentUser.getUsername());
            iTbMutualInspectiondetailService.insertByBo(tbMutualBo);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @SaCheckPermission("xha-lmt:mutualInspectiondetail:updateMutual")
    @Log(title = "修改互检任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/updateMutual")
    public R<Void> updateMutual(@Validated(AddGroup.class) @RequestBody Map map) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TbMutualInspectionBo tbMuBo = new TbMutualInspectionBo();
            tbMuBo.setId(map.get("mutualId") == null ? null : Long.parseLong((String) map.get("mutualId")));
            tbMuBo.setRepairStartDate(map.get("repairStartDate") == null ? null : formatter.parse(String.valueOf(map.get("repairStartDate"))));
            tbMutualInspectionService.updateByBo(tbMuBo);
            TbMutualInspectiondetailBo tbMutualBo = new TbMutualInspectiondetailBo();
            LoginUser currentUser = LoginHelper.getLoginUser();
            tbMutualBo.setID(map.get("id") == null ? null : Long.parseLong((String) map.get("id")));
            tbMutualBo.setMutualId(map.get("mutualId") == null ? null : Long.parseLong((String) map.get("mutualId")));
            tbMutualBo.setWorkName((String) map.get("workName"));
            tbMutualBo.setWorkKind((String) map.get("workKind"));
            tbMutualBo.setWorkCategoryId(map.get("workCategoryId") == null? null : Long.parseLong(map.get("workCategoryId").toString()));
            tbMutualBo.setWorkCategory((String) map.get("workCategory"));
            tbMutualBo.setWorkCategoryRemark((String) map.get("workCategoryRemark"));
            tbMutualBo.setCheckReapirReqire((String) map.get("checkReapirReqire"));
            tbMutualBo.setRemark((String) map.get("remark"));
            tbMutualBo.setWorkGroup((String) map.get("workGroup"));
            tbMutualBo.setWorkUser((String) map.get("workUser"));
            tbMutualBo.setWorkUser2((String) map.get("workUser2"));
            tbMutualBo.setTaskUrl((String) map.get("taskUrl"));
            tbMutualBo.setUpdateTime(new Date());
            tbMutualBo.setUpdateBy(currentUser.getUsername());

            iTbMutualInspectiondetailService.updateByBo(tbMutualBo);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    //查询互检任务
    @GetMapping("/list/selectMutual")
    public TableDataInfo<TbMutualInspectiondetailVo> selectMutual(Long mutualId, String lmtType, String lmtNumber, String status, PageQuery pageQuery) {
        return iTbMutualInspectiondetailService.selectMutual(mutualId, lmtType, lmtNumber, status, pageQuery);
    }
}
